<template>
  <div class="container">
    <el-button
      size="small"
      type="primary"
      @click="add"
      style="margin-bottom: 20px; float: right"
      >添加局级账户<i class="el-icon-circle-plus-outline"></i
    ></el-button>
    <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%"
      :row-class-name="xxxTableRowClassName"
      :header-cell-style="{
        background: 'rgba(61,157,255,0.05)',
        color: '#333333',
      }"
    >
      <el-table-column
        prop="userName"
        label="用户姓名"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="userPhone"
        label="手机号"
        width="width"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.userPhone | formatPhone }}
        </template>
      </el-table-column>
      <el-table-column
        prop="schoolNum"
        label="学校可添加数量"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            size="small"
            plain
            @click="compile($event, row)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" plain @click="del($event, row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="form.teacherId ? '修改账户' : '添加账户'"
      :visible.sync="dialogFormVisible"
      width="700px"
      :close-on-click-modal="false"
    >
      <el-form
        label-position="left"
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="190px"
      >
        <el-form-item label="姓名:" prop="userName">
          <el-input v-model="form.userName" style="width: 70%"></el-input>
        </el-form-item>
        <el-form-item label="手机号:" prop="userPhone">
          <el-input v-model="form.userPhone" style="width: 70%"></el-input>
        </el-form-item>
        <el-form-item label="学校可添加数量:" prop="schoolNum">
          <el-input-number
            v-model="form.schoolNum"
            @change="handleChange"
            :min="1"
            :max="10"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item v-if="!form.userId" label="地区代码:" prop="areaCode">
          <el-input
            style="width: 70%"
            label="地区代码"
            v-model="form.areaCode"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button :loading="disable" size="small" type="primary" @click="save"
          >{{ disable ? "加载中" : "确 定" }}
        </el-button>
      </div>
    </el-dialog>

    <pagination-page @size="changeSize" @page="changePage" :total="total" />
  </div>
</template>

<script>
import {
  bureauAccountList,
  editBureauAccount,
  delBureauAccount,
} from "@/api/BureauLevel/index.js";
import encryption from "@/utils/encryption";
import Decrypt from "@/utils/Decrypt";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      disable: false,
      total: -1,
      loading: false,
      page: {
        pageSize: 10,
        pageNum: 1,
      },
      dialogFormVisible: false,
      tableData: [],
      form: {
        userId: "",
        userName: "",
        userPhone: "",
        schoolNum: "",
        areaCode: "",
      },
      rules: {
        areaCode: [
          {
            required: true,
            message: "请输入地区代码",
          },
        ],
        userName: [
          {
            required: true,
            message: "请输入名字",
          },
          {
            min: 2,
            max: 10,
            message: "长度必须在2-10个之间",
            trigger: "blur", // 触发校验的时机是失去焦点
          },
        ],
        userPhone: [
          {
            required: true,
            message: "请输入手机号",
          },
          {
            min: 7,
            max: 11,
            message: "长度必须在7-11个之间",
            trigger: "blur", // 触发校验的时机是失去焦点
          },
        ],
      },
      isLoading: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getBureauAccountList();
  },
  filters: {
    formatPhone(val) {
      let array = Decrypt(val).split("");
      array.splice(3, 4, "****");
      let result = array.join("");
      return result;
    },
  },
  mounted() {},
  methods: {
    handleChange(value) {
    },
    picUpload(e) {
      if (e.code === 200) {
        this.form.pic = e.result.pic;
      } else if (e.code === 401) {
        this.$router.replace("/");
        this.$message.error("您的身份信息已过期，请重新登录");
        sessionStorage.removeItem("userInfo");
        sessionStorage.removeItem("token");
        this.$store.commit("user/SET_TOKEN", "");
      } else {
        this.$message.error(e.message);
      }
      this.$refs.ruleForm.validate(() => {});
      this.loading = false;
    },
    //获取列表
    getBureauAccountList() {
      let data = {
        ...this.page,
      };
      this.isLoading = true;
      bureauAccountList(data).then((res) => {
        if (res.code == 200) {
          this.tableData = res.result;
          this.total = res.total;
          this.isLoading = false;
        }
      });
    },
    //添加
    add() {
      this.dialogFormVisible = true;
      (this.form = {
        userId: "",
        userName: "",
        userPhone: "",
        schoolNum: "",
      }),
        this.$nextTick(() => {
          this.$refs.ruleForm.clearValidate();
        });
    },
    //编辑
    compile(evt, row) {
      let target = evt.target;
      if (target.nodeName == "SPAN") {
        target = evt.target.parentNode;
      }
      target.blur();
      this.dialogFormVisible = true;
      this.form = { ...row };
      this.form.userPhone = Decrypt(this.form.userPhone);
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate();
      });
    },
    save() {
      let data = {
        ...this.form,
        userPhone: encryption(this.form.userPhone),
      };

      // 对所有表单项进行统一校验
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          this.disable = true;

          editBureauAccount(data).then((res) => {
            if (res.code === 200) {
              this.dialogFormVisible = false;
              this.getBureauAccountList();
              this.$message.success("保存成功");
              this.disable = false;
            } else {
              this.disable = false;
              this.$message.error(res.message);
            }
          });
        } else {
          return false;
        }
      });
    },
    //删除
    del(evt, row) {
      let target = evt.target;
      this.$confirm("是否确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          let data = {
            userId: row.userId,
          };
          delBureauAccount(data).then((res) => {
            if (res.code === 200) {
              this.getBureauAccountList();
              this.$message.success("删除成功");
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },

    xxxTableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 !== 0) {
        return "statistics-warning-row";
      } else {
        return "";
      }
    },
    changeSize(size) {
      this.page.pageSize = size;
      this.getBureauAccountList();
    },
    changePage(page) {
      this.page.pageNum = page;
      this.getBureauAccountList();
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-table__row.statistics-warning-row {
  background: rgba(61, 157, 255, 0.05);
}
::v-deep .el-upload-list__item {
  display: none;
}
::v-deep .el-form-item__label {
  padding-left: 40px;
}
</style>
